package com.lagou.client;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

public class NoteApi {


    public static void main(String[] args) throws InterruptedException {
        ZkClient zkClient = new ZkClient("127.0.0.1:2181");
        System.out.println("会话被创建");

        String path = "/lg_zkClient_ep_1";
        boolean exists = zkClient.exists(path);
        if (!exists) {
            System.out.println("该节点不存在，创建临时节点");
            zkClient.createPersistent(path);
        }

        //监听该节点的数据变化
        zkClient.subscribeDataChanges(path, new IZkDataListener() {
            //数据变化
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
                System.out.println(s + "节点数据发生了变化，变化后的节点数据为：" + o);
            }

            //数据删除
            @Override
            public void handleDataDeleted(String s) throws Exception {
                System.out.println("节点：" + s + "被删除");
            }
        });

        //获取节点内容
        Object o = zkClient.readData(path);
        System.out.println(o);


        Thread.sleep(Integer.MAX_VALUE);

        //更新
        //zkClient.writeData(path,"4567");
        //Thread.sleep(1000);


        //删除
        //zkClient.delete(path);
        //Thread.sleep(1000);

    }

}
